<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close" :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="编码" prop="clienteleCode">
        <el-input v-model.trim="form.clienteleCode" autocomplete="off" maxlength="50" placeholder="请输入客户编码"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="clienteleName">
        <el-input v-model.trim="form.clienteleName" autocomplete="off" maxlength="50" placeholder="请输入客户名称"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input v-model.trim="form.contacts" autocomplete="off" maxlength="50" placeholder="请输入联系人"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model.trim="form.phone" autocomplete="off" maxlength="11" placeholder="请输入手机号码"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone">
        <el-input v-model.trim="form.telephone" autocomplete="off" maxlength="50" placeholder="请输入联系电话"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="form.email" autocomplete="off" maxlength="50" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="传真" prop="facsimile">
        <el-input v-model.trim="form.facsimile" autocomplete="off" maxlength="50" placeholder="请输入传真"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model.trim="form.address" type="textarea" autocomplete="off" maxlength="200" placeholder="请输入地址"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doAdd, doEdit } from '@/api/basics/clientele'

  export default {
    name: 'ClienteleEdit',
    data() {
      return {
        form: {
          clienteleCode: '',
          clienteleName: '',
        },
        rules: {
          clienteleCode: [{ required: true, trigger: ['blur', 'change'], message: '请输入编码' }],
          clienteleName: [{ required: true, trigger: ['blur', 'change'], message: '请输入名称' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '新增'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.clienteleId) {
              await doEdit(this.form).then((res) => this.callbackFun(res))
            } else {
              await doAdd(this.form).then((res) => this.callbackFun(res))
            }
          } else {
            return false
          }
        })
      },
      callbackFun(res) {
        if (res.success) {
          this.$baseMessage(res.msg, 'success')
          this.$emit('fetch-data')
          this.close()
        } else {
          this.$baseMessage(res.msg, 'error')
        }
      },
    },
  }
</script>
